<!--
 * @Author: wangshiyang
 * @Date: 2023-06-19 13:39:59
 * @LastEditors: wangshiyang
 * @LastEditTime: 2023-06-19 14:21:14
 * @Description: 请填写简介
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="referrer" content="no-referrer" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src='https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.js'></script>
    <link href='https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.css' rel='stylesheet' />
    <script src='https://unpkg.com/@antv/l7'></script>
    <style>
        * {
            margin: 0;
            padding: 0
        }

        #map {
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
        }

        #marker {
            text-align: center;
            background-color: #c882eb92;
            line-height: 50px;
            color: #fff;
            width: 50px;
            height: 50px;
            border-radius: 50%;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <div id="map">

    </div>
    <script>
        const { PolygonLayer, Popup, Marker, MarkerLayer } = L7;
        const token = 'pk.eyJ1IjoiY2hlbmdiZW5jaGFvIiwiYSI6ImNsODU3aGRiODA0Y2UzcHBzZmFlcmdqZ2sifQ.8k59W_pB_Riwe6o-MneRlA'
        mapboxgl.accessToken = token;
        const map = new mapboxgl.Map({
            container: 'map', // container id
            style: 'mapbox://styles/mapbox/streets-v11', // stylesheet location
            center: [114.30, 30.50], // starting position [lng, lat]
            zoom: 14, // starting zoom
            pitch: 70
        });

        const scene = new L7.Scene({
            id: 'map',
            map: new L7.Mapbox({
                mapInstance: map,
            }),
        });
        const element = document.createElement("div");
        element.id = "marker";

        scene.on('loaded', () => {

            const popup1 = new Popup({
                // 初始锚点经纬度
                lngLat: {
                    lng: 112.59,
                    lat: 28.12,
                },
                // Popup 标题
                title: '长沙',
                // Popup 内容
                html: '臭豆腐',
                offsets:[0,50]
            });
            const popup2 = new L7.Popup({
                lngLat: {
                    lng: 114.30,
                    lat: 30.50,
                },// Popup 标题
                title: '武汉',
                // Popup 内容
                html: '热干面',
                offsets:[0,50]
            }).setText("hello world")
            // marker基本使用
            // const marker = new L7.Marker()
            //     .setLnglat([114, 30])
            //     .setPopup(popup);
            // scene.addMarker(marker);

            // 使用html自定义marker
            // setLngLat指定位置
            // setPopup绑定popup
            // const marker = new Marker({
            //     element
            // }).setLnglat({ lng: 114.30, lat: 30.50 }).setPopup(popup);
            // scene.addMarker(marker)

            // 使用MarkerLayer统一管理标注
            const data = [
                {
                    lng: 114.30,
                    lat: 30.50,
                    name: '武汉',
                    popup:popup2
                }, {
                    lng: 112.59,
                    lat: 28.12,
                    name: "长沙",
                    popup:popup1
                }
            ]
            const markerLayer = new MarkerLayer({
            });
            for (let i = 0; i < data.length; i++) {
                const element = document.createElement('p');
                element.id = 'marker'
                const { lng, lat, name, popup } = data[i];
                element.innerHTML = name
                const marker = new Marker({
                    element
                }).setLnglat({ lng, lat }).setPopup(popup);
                console.log(marker);
                markerLayer.addMarker(marker)
            }
            scene.addMarkerLayer(markerLayer);
        });

    </script>
</body>

</html>